package com.cy.service.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cy.condition.BackRoleCondition;
import com.cy.dao.BackRoleDao;
import com.cy.entity.BackPurview;
import com.cy.entity.BackRole;
import com.cy.entity.BackRolePurView;
import com.cy.entity.BackUser;
import com.cy.service.BackRoleService;

@Service("backRoleService")
public class BackRoleServiceImpl implements BackRoleService<BackRole> {

	private BackRoleDao backRoleDao;
	
	public BackRole saveBackRoleDWR(BackRole backRole,String purviewStr){
		if(backRole.getId()==null){   //新增
			backRoleDao.saveObject(backRole);
			if(purviewStr!=null && purviewStr.trim().length()>0){
				for(String pStrId:purviewStr.split(",")){
					BackRolePurView backRolePurView=new BackRolePurView();
					BackPurview backPurview=new BackPurview();
					backPurview.setId(Long.parseLong(pStrId));
					backRolePurView.setPurview(backPurview);
					backRolePurView.setRole(backRole);
					backRole.getRolePurviewSet().add(backRolePurView);
				}		
			}
		}else{		//修改
			BackRole newBackRole=backRoleDao.getObject(BackRole.class, backRole.getId());
			backRoleDao.deleteRolePurviews(newBackRole.getId());		//删除中间表关联
			newBackRole.setName(backRole.getName());
			newBackRole.setRemark(backRole.getRemark());
			if(purviewStr!=null && purviewStr.trim().length()>0){
				for(String pStrId:purviewStr.split(",")){
					BackRolePurView backRolePurView=new BackRolePurView();
					BackPurview backPurview=new BackPurview();
					backPurview.setId(Long.parseLong(pStrId));
					backRolePurView.setPurview(backPurview);
					backRolePurView.setRole(newBackRole);
					newBackRole.getRolePurviewSet().add(backRolePurView);
				}
			}
			backRoleDao.saveObject(newBackRole);
		}
		return backRole;
	}
	
	public List<BackUser> findBackUserByRoleId(Long roleId) {
		return backRoleDao.findBackUserByRoleId(roleId);
	}
	
	public List<BackUser> findBackUserNotEqualRoleId(Long roleId) {
		return backRoleDao.findBackUserNotEqualRoleId(roleId);
	}
	
	
	public BackRole getObject(Serializable id) {
		return backRoleDao.getObject(BackRole.class, id);
	}

	public List<BackRole> listBackRoleByCondition(int firstResult,int maxResults, BackRoleCondition condition) {
		return backRoleDao.listBackRoleByCondition(firstResult, maxResults, condition);
	}
	
	public BackRole findDetailBackRoleById(Long id) {
		return backRoleDao.findDetailBackRoleById(id);
	}
	


	
	public List<BackRole> getAllObjects() {
		return backRoleDao.getObjects(BackRole.class);
	}

	public void removeObject(BackRole obj) {
		backRoleDao.removeObject(BackRole.class,obj.getId());
	}

	public BackRole saveObject(BackRole obj) {
		return null;
	}

	public BackRole updateObject(BackRole obj) {
		return null;
	}

	public BackRoleDao getBackRoleDao() {
		return backRoleDao;
	}
	
	@Autowired
	public void setBackRoleDao(BackRoleDao backRoleDao) {
		this.backRoleDao = backRoleDao;
	}

	








	
}
